// pages/HPStore/HPStoreDetail/HPStoreDetail.js
const App = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAgree:true,
    buy:false,
    //购买成功弹窗
    buySuccess:false,
    isClicked:true,
    djname:'定金',
    type:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type == 'bk'){
      this.setData({
        id:options.id,
        type:options.type,
        kuid:options.kuid,
        djname:'尾款',
      })
    }else{
      this.setData({
        id:options.id
      })
    }
    
    this.getDetailData(options.id)
  },
  //是否同意协议
  isAgress:function (){
    let isAgress = this.data.isAgree;
    if (isAgress) {
      this.setData({
        isAgree:false
      })
    }else{
      this.setData({
        isAgree:true
      })
    }
  },
  getDetailData:function (id){
    utils.requestUrl({
      // method:'post',
      url: "/niu/apiv3/niu_getdetail_pure.php",
      params: {
        markid: wx.getStorageSync('markId'),
        poolid:id
      }
    }).then((res) => {
      let data = res.data;
      console.log(data);
      let time = data.toy.book_time.substring(0,4)+'年'+data.toy.book_time.substring(4,6) + '月';
      var book_time = '';
      if (data.toy.book_time == 0){
        book_time = '无'
      }else{
        book_time = time;
      }
      var moneyrmbQK = Number(data.moneyrmb) + Number(data.bkmoneyrmb) 
      this.setData({
        moneyrmb:data.moneyrmb,
        bkmoneyrmb:data.bkmoneyrmb,
        moneyrmbQK:moneyrmbQK,
        toyspic:data.picbanner,
        danjitype:data.danjitype,
        toysname:data.name,
        intro:data.intro,
        price:data.toy.price,
        mainseries:data.toy.mainseries,
        book_time:book_time,
        sptype:data.toy.sptype,
        maker:data.toy.maker,
        secondseries:data.toy.secondseries
      })
    })
  },
  // 购买
  bugBtn:function (){
    if(wx.getStorageSync('markId') == ''){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      this.setData({
        buy:true
      })
    }
  },
  //取消购买
  cancel:function (){
    this.setData({
      buy:false
    })
  },
  //确认购买 (唤起微信支付)(直购商品)
  affirmBuy:function (){
    let that = this;
    var isClicked = that.data.isClicked;
    console.log(isClicked)
    if (isClicked){
      that.setData({
        isClicked:false
      })
      let isAgree = this.data.isAgree
      if (!isAgree) {
        utils.toast('请同意蛋酱商城交易协议')
        return
      }
      utils.requestUrl({
        url: "/niu/wxpay/wxprepay_wxsapp.php?paytype=hpstore",
        params: {
          markid: wx.getStorageSync('markId'),
          payforid: this.data.id,
          openid: wx.getStorageSync('openId')
        }
      }).then((res) => {
        var data = res.data;
        console.log(data);
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: 'MD5',
          paySign: data.paySign,
          success (res) {
            console.log(res)
            if(res.errMsg == 'requestPayment:ok'){
              that.setData({
                buy:false,
                isClicked:true,
                buySuccess:true
              })
            }
          },
          fail (res) {
            console.log(res)
            that.setData({
              isClicked:true,
            })
            utils.toast('支付失败,请重新支付')
          }
        })
      })
    }
    
  },
  //确认购买 (唤起微信支付)(直购商品)
  affirmBuyBK:function (){
    let that = this;
    var isClicked = that.data.isClicked;
    var moneycount = (this.data.bkmoneyrmb) / 100;
    console.log(wx.getStorageSync('markId'))
    console.log(moneycount)
    console.log( wx.getStorageSync('openId'))
    if (isClicked){
      that.setData({
        isClicked:false
      })
      let isAgree = this.data.isAgree
      if (!isAgree) {
        utils.toast('请同意蛋酱商城交易协议')
        return
      }
      utils.requestUrl({
        url: "/niu/wxpay/wxprepay_wxsapp.php?paytype=freepay",
        params: {
          markid: wx.getStorageSync('markId'),
          moneycount:moneycount,
          appsname:'djqy',
          openid: wx.getStorageSync('openId')
        }
      }).then((res) => {
        var data = res.data;
        console.log(data);
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: 'MD5',
          paySign: data.paySign,
          success (res) {
            console.log(res)
            if(res.errMsg == 'requestPayment:ok'){
              that.niuBukuanHp();
            }
          },
          fail (res) {
            console.log(res)
            that.setData({
              isClicked:true,
            })
            utils.toast('支付失败,请重新支付')
          }
        })
      })
    }
    
  },
  niuBukuanHp:function () {
    var kuid = this.data.kuid;
    var id = this.data.id;
    utils.requestUrl({
      url: "/niu/apiv4/niu_bukuan_hp.php",
      params: {
        markid:wx.getStorageSync('markId'),
        poolid:id,
        kutoysid:kuid,
        times:1,
        seed:0,
        money3:0,
      }
    }).then((res) => {
      var data = res.data;
      console.log(data);
      if (data.resultcode === '0'){
        wx.navigateBack({
          delta: 1,
        })
      } else {
          utils.toast(data.msg)
      }
     
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // if(wx.getStorageSync('markId') == ''){
    //   wx.navigateTo({
    //     url: '/pages/login/login',
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})